package org.springblade.tms.module.home.base;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import org.springblade.core.tool.api.R;
import org.springblade.tms.global.enums.UserTypeEnum;
import org.springblade.tms.global.enums.WaybillStatusEnum;
import org.springblade.tms.module.car.entity.TMCar;
import org.springblade.tms.module.car.service.ICarService;
import org.springblade.tms.module.demandorder.entity.TMDemandOrder;
import org.springblade.tms.module.demandorder.service.IDemandOrderService;
import org.springblade.tms.module.home.HomeVo;
import org.springblade.tms.module.monitor.vo.StatisticalParameters;
import org.springblade.tms.module.supercargoinfo.entity.TMSupercargoInfo;
import org.springblade.tms.module.supercargoinfo.service.ISupercargoInfoService;
import org.springblade.tms.module.transportorder.entity.TMTransportOrder;
import org.springblade.tms.module.transportorder.service.ITransportOrderService;
import org.springblade.tms.module.userinfo.base.DriverManagementService;
import org.springblade.tms.module.userinfo.entity.TMUserInfo;
import org.springblade.tms.module.userinfo.service.IUserInfoService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @projectName: BladeX
 * @package: org.springblade.tms.module.home.base
 * @className: HomeService
 * @author: yr
 * @description: 首页服务
 * @date: 2022/3/23 16:39
 */
@Service
@Transactional
public class HomeService {
	@Resource
	private IUserInfoService userInfoService;
	@Resource
	private ITransportOrderService transportOrderService;
	@Resource
	private ICarService carService;
	@Resource
	private ISupercargoInfoService supercargoInfoService;
	@Resource
	private DriverManagementService driverManagementService;
	@Resource
	private IDemandOrderService demandOrderService;
	/**
	 * @param :
	 * @return R<TransportationStatisticsVo>
	 * @author yr
	 * @description 首页
	 * @date 2022/3/23 16:41
	 */
	public R<HomeVo> home() {
		HomeVo homeVo = new HomeVo();
		//用户数据
		List<StatisticalParameters> userData = new ArrayList<>();
		List<TMUserInfo> userInfos = userInfoService.lambdaQuery().select(TMUserInfo::getUserType).list();
		Map<String, List<TMUserInfo>> userInfoMap = userInfos.stream().collect(Collectors.groupingBy(TMUserInfo::getUserType));
		for (UserTypeEnum typeEnum : UserTypeEnum.values()) {
			if (userInfoMap.get(typeEnum.getValue()) != null) {
				userData.add(new StatisticalParameters(typeEnum.getDescription(), userInfoMap.get(typeEnum.getValue()).size()));
			}else {
				userData.add(new StatisticalParameters(typeEnum.getDescription(),0));
			}
		}
		//运单数据
		//获取当前用户
		TMUserInfo tmUserInfo = driverManagementService.getTmUserInfo();
		List<StatisticalParameters> waybillData = new ArrayList<>();
		LambdaQueryChainWrapper<TMTransportOrder> tmTransportOrderQuery = transportOrderService.lambdaQuery().select(TMTransportOrder::getTransportStatus);
		if (tmUserInfo!=null){
			tmTransportOrderQuery = tmTransportOrderQuery.eq(tmUserInfo.getUserType().equals(UserTypeEnum.CARRIER.getValue()),
				TMTransportOrder::getCarrierId, tmUserInfo.getId());
		}
		List<TMTransportOrder> tmTransportOrders = tmTransportOrderQuery.list();
		Map<String, List<TMTransportOrder>> transportOrderMap = tmTransportOrders.stream().collect(Collectors.groupingBy(TMTransportOrder::getTransportStatus));
		for (WaybillStatusEnum statusEnum : WaybillStatusEnum.values()) {
			if (transportOrderMap.get(statusEnum.getValue()) != null) {
				waybillData.add(new StatisticalParameters(statusEnum.getDescription(), transportOrderMap.get(statusEnum.getValue()).size()));
			}else {
				waybillData.add(new StatisticalParameters(statusEnum.getDescription(), 0));
			}
		}
		LambdaQueryChainWrapper<TMDemandOrder> tmDemandOrderQuery = demandOrderService.lambdaQuery();
		if (tmUserInfo!=null){
			tmDemandOrderQuery = tmDemandOrderQuery.eq(tmUserInfo.getUserType().equals(UserTypeEnum.CARRIER.getValue()),
				TMDemandOrder::getCarrierId, tmUserInfo.getId());
		}
		List<TMDemandOrder> tmDemandOrders = tmDemandOrderQuery.list();
		waybillData.add(new StatisticalParameters("需求单总数",tmDemandOrders.size()));
		//车辆数据
		List<TMCar> tmCars = carService.lambdaQuery().list();
		homeVo.setVehicleData(new StatisticalParameters("车辆",tmCars.size()));
		//押运员数据
		List<TMSupercargoInfo> tmSupercargoInfos = supercargoInfoService.lambdaQuery().list();
		homeVo.setEscortData(new StatisticalParameters("押运员",tmSupercargoInfos.size()));
		homeVo.setUserData(userData);
		homeVo.setWaybillData(waybillData);
		return R.data(homeVo);
	}
}
